package com.yiwyn.Creational.builder;

import lombok.Data;

import java.io.Serializable;

public class Builder01Demo {

    public static void main(String[] args) {
        Builder petBuilder = new PetBuilder();
        Director director = new Director(petBuilder);
        Pet construct = director.construct("dog");
        System.out.println(construct);
    }


    /**
     * 宠物类
     */
    @Data
    static class Pet implements Serializable {
        private static final long serialVersionUID = -2890296999914201365L;

        private String petName;
        private Integer petAge;

    }

    /**
     * 创建者接口
     */
    interface Builder {

        Builder buildPetName(String name);

        Builder buildPetAge(Integer age);

        Pet createPet();
    }

    /**
     * 宠物创建者
     */
    static class PetBuilder implements Builder {

        private final Pet pet = new Pet();

        @Override
        public PetBuilder buildPetName(String name) {
            pet.setPetName(name);
            return this;
        }

        @Override
        public PetBuilder buildPetAge(Integer age) {
            pet.setPetAge(age);
            return this;
        }

        @Override
        public Pet createPet() {
            return pet;
        }
    }


    /**
     * 指挥者
     */
    static class Director {
        private final Builder builder;

        public Director(Builder builder) {
            this.builder = builder;
        }

        public Pet construct(String type) {
            if (type.equals("dog")) {
                builder.buildPetName("WatchDog");
                builder.buildPetAge(1);
                return builder.createPet();
            } else if (type.equals("cat")) {
                builder.buildPetName("TomCat");
                builder.buildPetAge(1);
                return builder.createPet();
            }
            return null;
        }

    }


}
